package com.tjjingpan.wifidirectdemo.broadcast

import android.Manifest
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.net.NetworkInfo
import android.net.wifi.p2p.WifiP2pDevice
import android.net.wifi.p2p.WifiP2pDeviceList
import android.net.wifi.p2p.WifiP2pManager
import android.util.Log
import androidx.core.app.ActivityCompat
import com.tjjingpan.wifidirectdemo.callback.DirectActionListener
import java.util.ArrayList

class DirectBroadcastReceiver(
    wifiP2pManager: WifiP2pManager,
    channel:WifiP2pManager.Channel,
    directActionListener: DirectActionListener)
    : BroadcastReceiver() {

    private val TAG = "DirectBroadcastReceiver"
    private val mWifiP2pManager: WifiP2pManager? = null
    private val mChannel: WifiP2pManager.Channel? = null
    private val mDirectActionListener: DirectActionListener? = null

    companion object{
        fun getIntentFilter(): IntentFilter? {
            val intentFilter = IntentFilter()
            intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION)
            intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION)
            intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION)
            intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION)
            return intentFilter
        }
    }


    override fun onReceive(context: Context, intent: Intent) {
         val action = intent.action
        if (action != null) {
            when (action) {
                WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION -> {
                    val state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -100)
                    if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
                        mDirectActionListener!!.wifiP2pEnabled(true)
                    } else {
                        mDirectActionListener!!.wifiP2pEnabled(false)
                        val wifiP2pDeviceList: ArrayList<WifiP2pDevice> = ArrayList()
                        mDirectActionListener.onPeersAvailable(wifiP2pDeviceList)
                    }
                }
                WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION -> {
                    if (ActivityCompat.checkSelfPermission(
                            context,
                            Manifest.permission.ACCESS_FINE_LOCATION
                        ) != PackageManager.PERMISSION_GRANTED
                    ) {
                        return
                    }
                    mWifiP2pManager!!.requestPeers(mChannel) { peers: WifiP2pDeviceList ->
                        mDirectActionListener!!.onPeersAvailable(
                            peers.deviceList
                        )
                    }
                }
                WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION -> {
                    val networkInfo =
                        intent.getParcelableExtra<NetworkInfo>(WifiP2pManager.EXTRA_NETWORK_INFO)
                    if (networkInfo != null && networkInfo.isConnected) {
                        mWifiP2pManager!!.requestConnectionInfo(
                            mChannel
                        ) { info -> mDirectActionListener!!.onConnectionInfoAvailable(info) }
                        Log.e(TAG, "已连接p2p设备")
                    } else {
                        mDirectActionListener!!.onDisconnection()
                        Log.e(TAG, "与p2p设备已断开连接")
                    }
                }
                WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION -> {
                    val wifiP2pDevice =
                        intent.getParcelableExtra<WifiP2pDevice>(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE)
                    if (wifiP2pDevice != null) {
                        mDirectActionListener!!.onSelfDeviceAvailable(wifiP2pDevice)
                    }
                }
            }
        }
    }
}